@echo off
chcp 936 >nul
setlocal

:: Force to script directory
pushd "%~dp0"

echo ==========================================
echo   Star Resonance Damage Counter Setup
echo ==========================================
echo Unified installation with smart detection
echo ==========================================
echo.

:: Step 1: Environment Check
echo [STEP 1] Checking environment...
if not exist "package.json" (
    echo ERROR: package.json not found in current directory
    echo Current path: %CD%
    echo Please ensure you are in the correct project folder
    pause
    exit /b 1
)

:: Check installation status
set "MAIN_OK=0"
set "OVERLAY_OK=0"

if exist "node_modules" (
    echo [OK] Main dependencies already installed
    set "MAIN_OK=1"
) else (
    echo [NEED] Main dependencies need installation
)

if exist "floating_overlay\node_modules" (
    echo [OK] Desktop overlay already installed
    set "OVERLAY_OK=1"
) else (
    echo [NEED] Desktop overlay need installation
)

:: Check if everything is already installed
if "%MAIN_OK%"=="1" (
    if "%OVERLAY_OK%"=="1" (
        echo.
        echo ==========================================
        echo     INSTALLATION ALREADY COMPLETED!
        echo ==========================================
        echo.
        echo [SUCCESS] All dependencies are ready
        echo [SUCCESS] Main application: Ready
        echo [SUCCESS] Desktop overlay: Ready
        echo.
        echo HOW TO START:
        echo 1. Double-click "start.bat" - Complete launcher
        echo 2. Double-click "一键启动.bat" - One-click startup
        echo 3. Access browser: http://localhost:8989
        echo.
        echo Press any key to exit...
        pause >nul
        popd
        exit /b 0
    )
)

:: Step 2: Node.js Check
echo.
echo [STEP 2] Checking Node.js...
where node >nul 2>&1
if %errorlevel% neq 0 (
    echo ERROR: Node.js not found
    echo Please install Node.js from: https://nodejs.org/
    echo Minimum version required: 18.0.0
    pause
    popd
    exit /b 1
)
echo OK: Node.js found

:: Step 3: Main Dependencies
echo.
echo [STEP 3] Installing main dependencies...
if "%MAIN_OK%"=="0" (
    echo Running: npm install with optimized settings...
    npm install >install_temp.log 2>&1
    if %errorlevel% neq 0 (
        echo ERROR: Main dependency installation failed
        echo.
        echo Recent error details:
        type install_temp.log | findstr /i "error npm" | more +0
        echo.
        echo Solutions:
        echo 1. Run as administrator
        echo 2. Check internet connection
        echo 3. Clear npm cache: npm cache clean --force
        echo.
        echo Full log saved to: install_temp.log
        pause
        popd
        exit /b 1
    )
    echo [SUCCESS] Main dependencies installed
    del install_temp.log >nul 2>&1
) else (
    echo [SKIP] Main dependencies already installed
)

:: Step 4: Desktop Overlay
echo.
echo [STEP 4] Installing desktop overlay...
if exist "floating_overlay" (
    if "%OVERLAY_OK%"=="0" (
        pushd "floating_overlay"
        if exist "package.json" (
            echo Running: npm install for desktop overlay...
            npm install >overlay_temp.log 2>&1
            if %errorlevel% neq 0 (
                echo [WARNING] Desktop overlay installation failed
                echo Desktop features will not be available
                echo.
                echo Error details:
                type overlay_temp.log | findstr /i "error" | more +0
            ) else (
                echo [SUCCESS] Desktop overlay installed
            )
            del overlay_temp.log >nul 2>&1
        ) else (
            echo [INFO] No overlay package.json found
        )
        popd
    ) else (
        echo [SKIP] Desktop overlay already installed
    )
) else (
    echo [INFO] Desktop overlay directory not found
)

:: Completion
echo.
echo ==========================================
echo         INSTALLATION COMPLETED!
echo ==========================================
echo.
echo [SUCCESS] Setup finished successfully
echo.
echo NEXT STEPS:
echo ==========================================
echo.
echo RECOMMENDED [Complete Experience]:
echo   → Double-click "一键启动.bat"
echo   → Automatic server + browser + overlay startup
echo.
echo ALTERNATIVE OPTIONS:
echo   → Double-click "start.bat" - Server only
echo   → Manual access: http://localhost:8989
echo.
echo TIPS:
echo • Run as administrator for best compatibility
echo • Ensure game is running before starting DPS tracking
echo • Check firewall if connection issues occur
echo.
echo Press any key to finish...
pause >nul
popd
exit /b 0
